package com.cskaoyan.service.wxgoods_service;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.comment.MarketComment;
import com.cskaoyan.bean.comment.MarketCommentExample;
import com.cskaoyan.bean.goods.*;
import com.cskaoyan.bean.goods.wxbean.*;
import com.cskaoyan.bean.market.Brand;
import com.cskaoyan.bean.market.Category;
import com.cskaoyan.bean.market.CategoryExample;
import com.cskaoyan.bean.market.Issue;
import com.cskaoyan.bean.system.MarketSystem;
import com.cskaoyan.bean.user.User;
import com.cskaoyan.mapper.*;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsAttributeMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsSpecificationMapper;
import com.cskaoyan.util.DateUtils;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hu
 * @create 2021-09-18 14:09
 * @Description
 */


@Service
public class WXGoodsServiceImpl implements WXGoodsService {


    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    @Autowired
    IssueMapper issueMapper;
    @Autowired
    MarketCommentMapper commentMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    MarketSystemMapper systemMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    CollectMapper collectMapper;


    /**
     * @return GoodsDetailVO
     * @Description //组装VO
     * @Param GoodsId
     */

    @Override
    public GoodsDetailVO detail(Integer id) {

        GoodsDetailVO goodsDetailVO = new GoodsDetailVO();
        //配置默认参数

        goodsDetailVO.setGroupon(new ArrayList<Object>());
        //setUserHasCollect 传user 查找





        MarketSystem marketSystem = systemMapper.selectByPrimaryKey(5);
        //为false时候 !false=false 为假
        //为true时候  !true=false 为真
        goodsDetailVO.setShare(!marketSystem.getKeyValue().equals("false"));




        goodsDetailVO.setShareImage(new String());

        //marketgoods
        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(id);
        goodsDetailVO.setInfo(marketGoods);


        //设置商品是否收藏
        //todo:user稍后从shiro获取
        int userId = 0;
        int isCollect = collectMapper.selectByUserAndGoods(userId, marketGoods.getId());
        goodsDetailVO.setUserHasCollect(0);
        if (isCollect==1){
            goodsDetailVO.setUserHasCollect(1);
        }


        //List<MarketGoodsProduct> productList;
        List<MarketGoodsProduct> marketGoodsProducts = productMapper.selectProductByGoodsId(id);
        goodsDetailVO.setProductList(marketGoodsProducts);


        //Brand brand
        Integer brandId = marketGoods.getBrandId();
        Brand brand = brandMapper.selectByPrimaryKey(brandId);
        goodsDetailVO.setBrand(brand);


        //List<MarketGoodsAttribute> attribute
        List<MarketGoodsAttribute> marketGoodsAttributes = attributeMapper.selectAttributeByGoodsId(id);
        goodsDetailVO.setAttribute(marketGoodsAttributes);


        //List<Issue> issue;所有商品通用的issue 直接返回遍历
        List<Issue> issues = issueMapper.selectByExample(null);
        goodsDetailVO.setIssue(issues);


//-------------------------------------------------------------------------------------
        //comment 手动实现---此处是
        //comment -type=0为商品评论 sql筛选加入条件
        MarketCommentExample marketCommentExample = new MarketCommentExample();
        MarketCommentExample.Criteria criteria = marketCommentExample.createCriteria();
        byte type = 0;
        criteria.andTypeEqualTo(type);
        criteria.andValueIdEqualTo(id);
        criteria.andDeletedEqualTo(false);
        List<MarketComment> marketComments = commentMapper.selectByExample(marketCommentExample);

        ArrayList<CommentDataVO> commentDataVOS = new ArrayList<>();

        if (marketComments.size() == 2) {

            //手动封评论1Data
            MarketComment comment1 = marketComments.get(0);
            CommentDataVO data1 = new CommentDataVO();
            //get user id 获取 user信息 并赋值 nickename 和 avatar
            data1.setAddTime(DateUtils.dateSimpleFormat(comment1.getAddTime()));
            data1.setPicList(comment1.getPicUrls());
            data1.setAdminContent(comment1.getAdminContent());
            data1.setId(comment1.getUserId());
            data1.setContent(comment1.getContent());
            User user1 = userMapper.selectByPrimaryKey(comment1.getUserId());
            data1.setNickname(user1.getNickname());
            data1.setAvatar(user1.getAvatar());

            //手动封评论1Data
            MarketComment comment2 = marketComments.get(1);
            CommentDataVO data2 = new CommentDataVO();
            //get user id 获取 user信息 并赋值 nickename 和 avatar
            data2.setAddTime(DateUtils.dateSimpleFormat(comment2.getAddTime()));
            data2.setPicList(comment2.getPicUrls());
            data2.setAdminContent(comment2.getAdminContent());
            data2.setId(comment2.getUserId());
            data2.setContent(comment2.getContent());
            User user2 = userMapper.selectByPrimaryKey(comment2.getUserId());
            data2.setNickname(user2.getNickname());
            data2.setAvatar(user2.getAvatar());

            commentDataVOS.add(data1);
            commentDataVOS.add(data2);

        } else if (marketComments.size() == 1) {
            MarketComment comment1 = marketComments.get(0);
            CommentDataVO data1 = new CommentDataVO();
            //get user id 获取 user信息 并赋值 nickename 和 avatar
            data1.setAddTime(DateUtils.dateSimpleFormat(comment1.getAddTime()));
            data1.setPicList(comment1.getPicUrls());
            data1.setAdminContent(comment1.getAdminContent());
            data1.setId(comment1.getUserId());
            data1.setContent(comment1.getContent());
            User user1 = userMapper.selectByPrimaryKey(comment1.getUserId());
            data1.setNickname(user1.getNickname());
            data1.setAvatar(user1.getAvatar());
            commentDataVOS.add(data1);

        }

        //直接赋给comment类中的内部类goodsDetailVO代替赋值
        goodsDetailVO.getComment().setData(commentDataVOS);
        goodsDetailVO.getComment().setCount(marketComments.size());

//-------------------------------------------------------------------------------------

        SpecificationListVO specificationListVO = new SpecificationListVO();
        List<MarketGoodsSpecification> marketGoodsSpecifications = specificationMapper.selectSpecByGoodsId(id);

        specificationListVO.setValueList(marketGoodsSpecifications);
        String specification = specificationListVO.getValueList().get(0).getSpecification();
        specificationListVO.setName(specification);
        ArrayList<SpecificationListVO> specificationListVOS = new ArrayList<>();
        specificationListVOS.add(specificationListVO);
        //specificationList 手动实现

        goodsDetailVO.setSpecificationList(specificationListVOS);

        return goodsDetailVO;
    }


    @Override
    public BaseData related(Integer id) {
        //此处BaseData中的page数据是给死的直接赋值
        BaseData<MarketGoods> marketGoodsBaseData = new BaseData<>();

        MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(id);
        //类目ID 查询同类项
        Integer categoryId = marketGoods.getCategoryId();

        //limit 6 page 1  total  pages
        BaseParam baseParam = new BaseParam();
        baseParam.setPage(1);
        baseParam.setLimit(6);

        PageUtil.start(baseParam);
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andCategoryIdEqualTo(categoryId);
        List<MarketGoods> goods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
        BaseData<MarketGoods> goodsBaseData = new BaseData<>(pageInfo, goods);
        return goodsBaseData;
    }

    @Override
    public Integer count() {
        //查找非产出项首页订单
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> goods = goodsMapper.selectByExample(marketGoodsExample);
        int count = goods.size();
        return count;
    }

    @Override
    public CategoryVO category(Integer id) {
        //传入id接收currentCategory
        //根据pid 找到parentCategory  根绝pid找到brotherCategory
        CategoryVO categoryVO = new CategoryVO();


        Category category = categoryMapper.selectByPrimaryKey(id);
        categoryVO.setCurrentCategory(category);//VO参数1

        String level = category.getLevel();//目标层级level 分L1 L2?????

        //初始化example条件
        CategoryExample categoryExample = new CategoryExample();
        CategoryExample.Criteria criteria = categoryExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(id);


        //分类的三种逻辑判断
        //1 L2目录
        //2 L1目录有子类
        //3 L1目录无子类
        if ("L2".equals(level)) {
            //查询同为L2兄弟目录
            CategoryExample tempExample = new CategoryExample();
            CategoryExample.Criteria tempCriteria = tempExample.createCriteria();
            tempCriteria.andDeletedEqualTo(false);
            tempCriteria.andPidEqualTo(category.getPid());
            List<Category> categories = categoryMapper.selectByExample(tempExample);
            categoryVO.setBrotherCategory(categories);//VO参数2

            Integer pid = category.getPid();
            categoryVO.setParentCategory(categoryMapper.selectByPrimaryKey(pid));//VO参数3
        } else if ("L1".equals(level)) {

            CategoryExample tempExample = new CategoryExample();
            CategoryExample.Criteria tempCriteria = tempExample.createCriteria();
            tempCriteria.andDeletedEqualTo(false);
            tempCriteria.andPidEqualTo(id);
            List<Category> categories = categoryMapper.selectByExample(tempExample);

            //如何该分类下没有L2的子类时
            if (categories.equals(null)) {
                categoryVO.setParentCategory(category);
                categoryVO.setBrotherCategory(null);
                return categoryVO;
            }

            categoryVO.setParentCategory(categories.get(0));
            categoryVO.setBrotherCategory(categories);

        }


        return categoryVO;
    }

    @Override
    public BaseData list(BaseParam baseParam ,Integer categoryId, Integer brandId) {
        //TODO:baseparam应根据system表格显示 page limit


        baseParam = new BaseParam();
        baseParam.setLimit(10);
        baseParam.setPage(1);
        PageUtil.start(baseParam);


        //判断brandId如果为0 直接返回一个goodlist
        if (brandId!=null){
//            GoodsInBrandListVO goodsInBrandListVO = new GoodsInBrandListVO();

            ArrayList<Category> categories = new ArrayList<>();


            MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
            criteria.andDeletedEqualTo(false);
            criteria.andBrandIdEqualTo(brandId);
            //filterCategoryList
            List<MarketGoods> goods = goodsMapper.selectByExample(marketGoodsExample);
            for (MarketGoods good : goods) {
                Category tempCat = categoryMapper.selectByPrimaryKey(good.getCategoryId());
                categories.add(tempCat);
            }
            //对list分页操作
            PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
            BaseData<MarketGoods> data = new BaseData<>(pageInfo, goods);
//            goodsInBrandListVO.setList(goods);//商品list
//            goodsInBrandListVO.setFilterCategoryList(categories);//父类cate list

            return data;
        }



        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        criteria.andCategoryIdEqualTo(categoryId);
        List<MarketGoods> goods = goodsMapper.selectByExample(marketGoodsExample);
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
        BaseData<MarketGoods> goodsBaseData = new BaseData<>(pageInfo, goods);
//        DuBaseData<MarketGoods, Object> goodsBaseData = new DuBaseData<>(pageInfo, goods, null);
        return goodsBaseData;
    }
}
